//
//  ForgetPassWordController.m
//  ShrimpCrowdordering
//
//  Created by 骆锴 on 2018/8/24.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ForgetPassWordController.h"

@interface ForgetPassWordController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UITextField *yanzhengma;
@property (nonatomic, strong) UITextField *changePassWord;
@property (nonatomic, strong) UITextField *confirmPassWord;

@end

@implementation ForgetPassWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKey)];
    [self.view addGestureRecognizer:tap];
    
    UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), SCREEN_WIDTH, ScreenX375(49))];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = Kfont(18);
    [saveBtn addTarget:self action:@selector(sentValue) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveBtn];
    
    [self setTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)setTableView{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row > 0 ) {
        static NSString *cellId = @"setCell";
        NSArray *titleValue = [[NSArray alloc] initWithObjects:@"",@"验证码",@"新密码",@"确认新密码", nil];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        UILabel *titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(16), ScreenX375(100), ScreenX375(15))];
        titleLabel.text = [titleValue objectAtIndex:indexPath.row];
        titleLabel.font = Kfont(15);
        titleLabel.textColor = REXADECIMALCOLOR(@"#333333");
        titleLabel.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:titleLabel];
        
        NSArray *placeHold = [[NSArray alloc] initWithObjects:@"",@"请输入验证码",@"请输入新密码",@"请在此输入新密码", nil];
        if (indexPath.row == 1) {
            UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
            UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
            NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
            [bar setItems:buttonsArray];
            
            _yanzhengma = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(220), ScreenX375(15))];
            _yanzhengma.font = Kfont(15);
            _yanzhengma.textAlignment = NSTextAlignmentLeft;
            _yanzhengma.placeholder = [placeHold objectAtIndex:indexPath.row];
            _yanzhengma.secureTextEntry = YES;
            _yanzhengma.inputAccessoryView = bar;
            [cell addSubview:_yanzhengma];
        }else if(indexPath.row == 2){
            UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
            UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
            NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
            [bar setItems:buttonsArray];
            
            _changePassWord = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(220), ScreenX375(15))];
            _changePassWord.font = Kfont(15);
            _changePassWord.textAlignment = NSTextAlignmentLeft;
            _changePassWord.placeholder = [placeHold objectAtIndex:indexPath.row];
            _changePassWord.secureTextEntry = YES;
            _changePassWord.inputAccessoryView = bar;
            [cell addSubview:_changePassWord];
        }else{
            UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
            UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
            UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
            NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
            [bar setItems:buttonsArray];
            
            _confirmPassWord = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(220), ScreenX375(15))];
            _confirmPassWord.font = Kfont(15);
            _confirmPassWord.textAlignment = NSTextAlignmentLeft;
            _confirmPassWord.placeholder = [placeHold objectAtIndex:indexPath.row];
            _confirmPassWord.secureTextEntry = YES;
            _confirmPassWord.inputAccessoryView = bar;
            [cell addSubview:_confirmPassWord];
        }
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(47.5), ScreenX375(359), ScreenX375(0.5))];
        line.backgroundColor = LineColor;
        [cell addSubview:line];
        
        return cell;
    }else{
        static NSString *cellId = @"phoneCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        UILabel *titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(16), ScreenX375(100), ScreenX375(15))];
        titleLabel.text = @"手机号";
        titleLabel.font = Kfont(15);
        titleLabel.textColor = REXADECIMALCOLOR(@"#333333");
        titleLabel.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:titleLabel];
        
        UILabel *phoneNumber =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(100), ScreenX375(15))];
        phoneNumber.text = self.phone;
        phoneNumber.font = Kfont(15);
        phoneNumber.textColor = REXADECIMALCOLOR(@"#333333");
        phoneNumber.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:phoneNumber];
        
        UIButton *getYZM =[[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(279), ScreenX375(13), ScreenX375(80), ScreenX375(24))];
        [getYZM setTitle:@"获取验证码" forState:UIControlStateNormal];
        [getYZM setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [getYZM setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
        [getYZM addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
        getYZM.titleLabel.font = Kfont(12);
        getYZM.layer.cornerRadius = 12.0;
        getYZM.clipsToBounds = YES;
        [cell addSubview:getYZM];

        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(47.5), ScreenX375(359), ScreenX375(0.5))];
        line.backgroundColor = LineColor;
        [cell addSubview:line];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScreenX375(48);
}

-(void)getCode{
    NSDictionary *parameter = @{@"phone":self.phone};
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/sendSms",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([code isEqualToString:@"200"]) {
            NSLog(@"%@",successResponse);
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(void)sentValue{
    if (![_changePassWord.text isEqualToString:_confirmPassWord.text]) {
        [HttpManager showNoteMsg:@"两次密码不相同"];
    }else{
        NSDictionary *parameter = @{@"userId":kNSUDefaultReadKey(@"userId"),@"phone":self.phone,@"code":_yanzhengma.text,@"newPassword":_changePassWord.text};
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/forgetPassword",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
            NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
            if ([code isEqualToString:@"200"]) {
                [HttpManager showNoteMsg:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }
}

-(void)resignKey{
    [_yanzhengma resignFirstResponder];
    [_changePassWord resignFirstResponder];
    [_confirmPassWord resignFirstResponder];
}
@end
